package com.huiquan.vocab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.vocab.domain.VocabWildcard;
import com.huiquan.vocab.service.VocabElementWildcardService;

@Controller
public class VocabElementWildcardController {

    @Autowired
    private VocabElementWildcardService vocabElementWildcardService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/element_wildcard_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("checkFlag", "0");
			
			List<VocabWildcard> vwList = vocabElementWildcardService.getAllVocabWildcard();
			if(!vwList.isEmpty()) {
				VocabWildcard vw = vwList.get(0);
				pageParam.put("wildcardKey", vw.getName() + "-" + vw.getProperty());
			}

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String vocabularyKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "vocabularyKey");
		String wildcardKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "wildcardKey");
		String checkFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "checkFlag");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return vocabElementWildcardService.list(start, vocabularyKey, wildcardKey, checkFlag, exactSearchFlag, countPerPageStr);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_wildcard_check_page.do")
	public ReturnData checkPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String checkedIds = req.getParameter("checkedIds");
		String unCheckedIds = req.getParameter("unCheckedIds");
		return vocabElementWildcardService.checkPage(checkedIds, unCheckedIds, user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/wildcard_get_list.do")
	public ReturnData getWildcardList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		return vocabElementWildcardService.getWildcardList();
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/wildcard_add.do")
	public ReturnData addWildcard(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name").trim();
		String property = req.getParameter("property").trim();
		return vocabElementWildcardService.addWildcard(name, property);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/wildcard_delete.do")
	public ReturnData deleteWildcard(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		return vocabElementWildcardService.deleteWildcard(id);
	}

}
